data<-read.csv("figure10.csv",header=T,as.is=T)

dataVotes<-read.csv("votes114.csv",header=T,as.is=T)
dataVotes<-unique(dataVotes[,1:3])

#75.2%
prop.table(table(dataVotes$Answer.dems[dataVotes$Answer.dems<100]<25))
#51.3%
prop.table(table(dataVotes$Answer.dems[dataVotes$Answer.dems<100]<8))
#25.66%
prop.table(table(dataVotes$Answer.dems[dataVotes$Answer.dems<100]<4))

data<-data[data$Q6.2==8,]
## pid3
data$pid3 <-NA
data$pid3[data$Q4.3==1] =1
data$pid3[data$Q4.3==2] =2
data$pid3[data$Q4.3>=3] =3
data$pid3 = factor(data$pid3 , labels=c("Republican","Democrat","Independent"))
table(data$pid3)

df <- read.csv(text="col1,col2")

#age
df<-rbind(df,cbind("Age",round(mean(data$Q1.4+17),digits=2)))
#male
df<-rbind(df,cbind("Male",round(prop.table(table(data$Q1.5))[[1]]*100,digits=2)))
#>college
df<-rbind(df,cbind(">College",round(prop.table(table(data$Q1.6>=7))[[2]]*100,digits=2)))
#white
df<-rbind(df,cbind("White",round(prop.table(table(data$Q1.8==4))[[2]]*100,digits=2)))
#ideology
df<-rbind(df,cbind("Ideology",round(mean(data$Q4.8),digits=2)))
names(df)<-c("Variable"," ")
print(xtable(df),include.rownames=FALSE)

data$condition<-NA
data$condition[data$random<3]<-"Control"
data$condition[data$random<7 & data$random>2]<-"\"Important\" Bill"
data$condition[data$random>6]<-"\"Bipartisan\" Bill"

########### 
# Figure 10
###########

t<-t.test(data$Q2.5[data$condition=="Control"])

e <- ggplot(data[data$condition!="Control",], aes(reps,Q2.5, group=condition, color = condition)) + 
	scale_y_continuous(limits = c(1, 7),expand = c(0, 0)) + 
	scale_x_continuous(limits = c(1, 100),expand = c(0, 0),breaks=seq(0, 100, by=5)) +
	geom_ribbon(aes(ymin=t$conf.int[1],ymax=t$conf.int[2], fill="Partisan Bill\n (0 Democrats)"),alpha=0.8,color=NA) +  
	geom_abline(intercept = t$estimate[[1]], slope = 0,color="#ffffff",alpha=1) +
	ggtitle ("(A)") + stat_smooth(method="loess",aes(fill=condition), alpha=0.6) +
	theme_bw() +
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  + 
	xlab("Number of Democrats Voting for Budget") + 
	ylab("Support for Legislation") + 
	scale_colour_manual(values = c("#ffffff","#ffffff","#ffffff","#000000","#000000"), guide = FALSE) + 
	scale_fill_manual(values = c("#3182bd","#e34a33","#feb24c"),guide = guide_legend(title = "Condition")) + 
	geom_vline(xintercept=c(4,8,25),linetype="dotted",color="#707070") + 
	annotate("text", size=3, x = 5, y = 3.1, label = "25.7% of votes",hjust = 0) +
	annotate("text", size=3, x = 9, y = 3.3, label = "51.3% of votes",hjust = 0) +
	annotate("text", size=3, x = 26, y = 3.5, label = "75.2% of votes",hjust = 0) +
	annotate("segment",x=25,xend=1,y=3.5,yend=3.5,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +  
	annotate("segment",x=8,xend=1,y=3.3,yend=3.3,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +  
	annotate("segment",x=4,xend=1,y=3.1,yend=3.1,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +
	coord_cartesian(ylim=c(3,6)) +
	theme(legend.position="bottom")
e

ggsave(filename="f10a.pdf", plot=e,width=6.5,height=5)


m<-lm(Q2.5~reps,data=data[data$condition=="\"Bipartisan\" Bill",])
eff<-effect(m,term="reps",se=T,xlevels=list(x1=c(1,100)))

dataeff<-as.data.frame(eff)
dataeff<-dataeff[c(1,5),]
dataeff

datacontrol<-as.data.frame(cbind(0,t$estimate[[1]],0,t$conf.int[1],t$conf.int[2]))
names(datacontrol)<-names(dataeff)
dataeff<-rbind(dataeff,datacontrol)

4.5/3.98
4.76/4.5
(4.76-4.5)/99

dataeff$reps <- factor(dataeff$reps, labels=c("Partisan Bill\n (0 Democrats)","\"Bipartisan\" Bill \n+ 1 Democrat","\"Bipartisan\" Bill \n+ 100 Democrats"))

d<- qplot(reps,fit,data=dataeff,color=reps,height=200) +
	geom_errorbar(aes(ymin=lower, ymax=upper),width=.25) + 
	coord_cartesian(ylim=c(3,6)) +
	theme_bw() +
	ggtitle("(B)")+
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  + 
	xlab("") + 
	ylab("") + 
	scale_color_manual(values = c("#feb24c","#3182bd","#3182bd"),guide = FALSE) +
	theme(axis.text.x = element_text(angle = 90, hjust = 1),axis.text.y=element_blank()) +
	theme(plot.margin = unit(c(.475, .25, 0, -.5), "cm")) +
	annotate("segment",x=1.5,xend=1.5,y=dataeff$fit[3],yend=dataeff$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=1.5,xend=1,y=dataeff$fit[3],yend=dataeff$fit[3],size=0.3,color="#707070") +
	annotate("segment",x=1.5,xend=1.95,y=dataeff$fit[1],yend=dataeff$fit[1],size=0.3,arrow=arrow(length=unit(0.2,"cm"),type="closed"),color="#707070") +
	annotate("segment",x=2.5,xend=2.5,y=dataeff$fit[2],yend=dataeff$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=2.5,xend=2,y=dataeff$fit[1],yend=dataeff$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=2.5,xend=2.95,y=dataeff$fit[2],yend=dataeff$fit[2],size=0.3,arrow=arrow(length=unit(0.2,"cm"),type="closed"),color="#707070") +
	annotate("text", size=3, x = 1.2, y = 3.6, label = paste(round(((dataeff$fit[1]/dataeff$fit[3])-1)*100, digits=2), "%\nmore \nsupport",sep=""),hjust = 0) +
	annotate("text", size=3, x = 2.2, y = 4.58, label = paste(round(((dataeff$fit[2]/dataeff$fit[1])-1)*100, digits=2), "%\nmore \nsupport",sep=""),hjust = 0) 


d

ggsave(filename="f10b.eps", plot=d,width=2,height=5.22)


t<-t.test(data$Q2.5[data$condition=="Control"], data$Q2.5[data$condition=="\"Bipartisan\" Bill" & data$reps==1])


########### 
# Figure 11
###########

t2<-t.test(data$Q2.6[data$condition=="Control"])

e2 <- ggplot(data[data$condition!="Control",], aes(reps,Q2.6, group=condition, color = condition)) + 
	scale_y_continuous(limits = c(1, 7),expand = c(0, 0)) + 
	scale_x_continuous(limits = c(1, 100),expand = c(0, 0),breaks=seq(0, 100, by=5)) +
	geom_ribbon(aes(ymin=t2$conf.int[1],ymax=t2$conf.int[2], fill="Partisan Bill\n (0 Democrats)"),alpha=0.8,color=NA) +  
	geom_abline(intercept = t2$estimate[[1]], slope = 0,color="#ffffff",alpha=1) +
	ggtitle ("(A)") + stat_smooth(method="loess",aes(fill=condition), alpha=0.6) +
	theme_bw() +
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  + 
	xlab("Number of Democrats Voting for Budget") + 
	ylab("Legislaton's Ideological Placement") + 
	scale_colour_manual(values = c("#ffffff","#ffffff","#ffffff","#000000","#000000"), guide = FALSE) + 
	scale_fill_manual(values = c("#3182bd","#e34a33","#feb24c"),guide = guide_legend(title = "Condition")) + 
	geom_vline(xintercept=c(4,8,25),linetype="dotted",color="#707070") + 
	annotate("text", size=3, x = 5, y = 3.1, label = "25.7% of votes",hjust = 0) +
	annotate("text", size=3, x = 9, y = 3.3, label = "51.3% of votes",hjust = 0) +
	annotate("text", size=3, x = 26, y = 3.5, label = "75.2% of votes",hjust = 0) +
	annotate("segment",x=25,xend=1,y=3.5,yend=3.5,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +  
	annotate("segment",x=8,xend=1,y=3.3,yend=3.3,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +  
	annotate("segment",x=4,xend=1,y=3.1,yend=3.1,size=0.3,arrow=arrow(length=unit(0.2,"cm"),ends="both",type="closed"),color="#707070") +
	coord_cartesian(ylim=c(3,6)) +
	theme(legend.position="bottom")
e2

ggsave(filename="f11a.pdf", plot=e2,width=6.5,height=5)

m2<-lm(Q2.6~reps,data=data[data$condition=="\"Bipartisan\" Bill",])

eff2<-effect(m2,term="reps",se=T,default.levels=100)


dataeff2<-as.data.frame(eff2)
dataeff2<-dataeff2[c(1,5),]


(5.09-4.83)/99

datacontrol2<-as.data.frame(cbind(0,t2$estimate[[1]],0,t2$conf.int[1],t2$conf.int[2]))
names(datacontrol2)<-names(dataeff2)
dataeff2<-rbind(dataeff2,datacontrol2)

(dataeff2$fit[2]-dataeff2$fit[1])/99

dataeff2$reps <- factor(dataeff2$reps, labels=c("Partisan Bill\n (0 Democrats)","\"Bipartisan\" Bill \n+ 1 Democrat","\"Bipartisan\" Bill \n+ 100 Democrats"))

d2<- qplot(reps,fit,data=dataeff2,color=reps,height=200) +
	geom_errorbar(aes(ymin=lower, ymax=upper),width=.25) + 
	coord_cartesian(ylim=c(3,6)) +
	theme_bw() +
	ggtitle("(B)")+
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  + 
	xlab("") + 
	ylab("") + 
	scale_color_manual(values = c("#feb24c","#3182bd","#3182bd"),guide = FALSE) +
	theme(axis.text.x = element_text(angle = 90, hjust = 1),axis.text.y=element_blank()) +
	theme(plot.margin = unit(c(.475, .25, 0, -.5), "cm")) +
	annotate("segment",x=1.5,xend=1.5,y=dataeff2$fit[3],yend=dataeff2$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=1.5,xend=1,y=dataeff2$fit[3],yend=dataeff2$fit[3],size=0.3,color="#707070") +
	annotate("segment",x=1.5,xend=1.95,y=dataeff2$fit[1],yend=dataeff2$fit[1],size=0.3,arrow=arrow(length=unit(0.2,"cm"),type="closed"),color="#707070") +
	annotate("segment",x=2.5,xend=2.5,y=dataeff2$fit[2],yend=dataeff2$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=2.5,xend=2,y=dataeff2$fit[1],yend=dataeff2$fit[1],size=0.3,color="#707070") +
	annotate("segment",x=2.5,xend=2.95,y=dataeff2$fit[2],yend=dataeff2$fit[2],size=0.3,arrow=arrow(length=unit(0.2,"cm"),type="closed"),color="#707070") +
	annotate("text", size=3, x = .7, y = 4.24, label = paste(round(((dataeff2$fit[1]/dataeff2$fit[3])-1)*-100, digits=2), "%\nless \nconservative",sep=""),hjust = 0) +
	annotate("text", size=3, x = 2.2, y = 3.86, label = paste(round(((dataeff2$fit[2]/dataeff2$fit[1])-1)*-100, digits=2), "%\nless \nconservative",sep=""),hjust = 0) 


d2

ggsave(filename="f11b.eps", plot=d2,width=2,height=5.22)

###############
# Figure 12
###############

dataKnow<-data
dataKnow$know <-NA
dataKnow$know <- (dataKnow$numRepsCorrect + dataKnow$termCorrect + dataKnow$repsVoteCorrect)/3

dataKnow$condition[data$condition=="Control"]<-"Partisan Bill (0 Democrats)"

dataKnow$repsPercent <- (dataKnow$reps/188) * 100

t3<-t.test(dataKnow$Q2.7_1[dataKnow$condition=="Partisan Bill (0 Democrats)"])

p <- ggplot(dataKnow[dataKnow$condition!="Partisan Bill (0 Democrats)" & !is.na(dataKnow$condition),],aes(repsPercent, Q2.7_1,color=condition))+
	scale_y_continuous(limits = c(1, 100),expand = c(0, 0),breaks=seq(0, 100, by=10)) + 
	scale_x_continuous(limits = c(1, 53),expand = c(0, 0),breaks=seq(0, 55, by=10))+
	stat_smooth(method="loess",aes(fill=condition), alpha=0.8) +
	geom_ribbon(aes(ymin=t3$conf.int[1],ymax=t3$conf.int[2], fill="Partisan Bill\n (0 Democrats)"),alpha=0.8,color=NA) +  
	geom_abline(intercept = t3$estimate[[1]], slope = 0,color="#ffffff") +
	#ggtitle("Relationship between Democratic\n Votes and Allocated Credit") +
	xlab("Percent of Democrats Reported to \nVote for the Legislation") + 
	ylab("Percent of Responsibility Allocated\nto Democrats") +
	theme_bw() +
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  +
	scale_colour_manual(values = c("#ffffff","#ffffff","#ffffff","#000000","#000000"), guide = FALSE) + 
	scale_fill_manual(values = c("#3182bd","#e34a33","#feb24c"),guide = guide_legend(title = "Condition")) + 
	theme(legend.position="bottom")+
	geom_abline(intercept = 0, slope = 1,color="#000000") +
	facet_grid(. ~ condition) +
	coord_cartesian(ylim=c(1,52),xlim=c(1,52)) 
p

ggsave(filename="f12.pdf", plot=p,width=6.5,height=4.5)

###############
# Figure 13
###############

se <- function(x) sd(x, na.rm=T)/sqrt(length(na.omit(x)))
results<- ddply(.data = dataKnow, .variables = .(condition,know), .fun = summarise, 
				mean = mean(Q2.5, na.rm=T),
				lower = mean(Q2.5, na.rm=T) - (1.96*se(Q2.5)),
				upper = mean(Q2.5, na.rm=T) + (1.96*se(Q2.5)),
				N = length(na.omit(Q2.5)))

results<-results[1:12,]

results$know <- factor(results$know, labels=c("0%","33%","66%","100%"))

biMean <-mean(dataKnow$Q2.5[dataKnow$condition=="\"Bipartisan\" Bill"],na.rm=T)
importantMean <-mean(dataKnow$Q2.5[dataKnow$condition=="\"Important\" Bill"],na.rm=T)
controlMean <-mean(dataKnow$Q2.5[dataKnow$condition=="Partisan Bill (0 Democrats)"],na.rm=T)

k<- qplot(know,mean,data=results,height=200) +
	geom_errorbar(aes(ymin=lower, ymax=upper),width=.25) + 
	scale_y_continuous(limits = c(2.6, 6))+
	theme_bw() +
	#ggtitle("Support for Legislation by\n Condition and Respondent Knowledge")+
	theme(panel.margin = unit(1, "lines")) + 
	theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background=element_blank(), axis.title.x = element_text(vjust=-0.5))  + 
	xlab("Congressional Knoweldge\n Index (3 item)") + 
	ylab("Support for Legislation") + 
	coord_flip()+
	geom_hline(aes(yintercept =biMean),alpha=.5,linetype="dashed",data=subset(results,condition=="\"Bipartisan\" Bill"))+
	geom_hline(aes(yintercept =importantMean),alpha=.5,linetype="dashed",data=subset(results,condition=="\"Important\" Bill"))+
	geom_hline(aes(yintercept =controlMean),alpha=.5,linetype="dashed",data=subset(results,condition=="Partisan Bill (0 Democrats)"))+
	facet_grid(. ~ condition)
k

ggsave(filename="f13.pdf", plot=k,width=7,height=2.25)


##### Het

data <- data[data$condition!= "\"Important\" Bill",]
data$pid3 <- relevel(data$pid3, "Independent")
data$condition <-as.factor(data$condition)
data$condition <- factor(data$condition,levels(data$condition)[c(2,3,1)])

data$dv<-data$Q2.5
demsupp <- lm(dv~condition +reps, data=data[data$pid3=="Republican",])
repsupp <- lm(dv~condition + reps, data=data[data$pid3=="Democrat",])
data$dv<-data$Q2.6
demideo <- lm(dv~condition + reps, data=data[data$pid3=="Republican",])
repideo <- lm(dv~condition + reps, data=data[data$pid3=="Democrat",])

stargazer(demsupp, repsupp, demideo, repideo,
		  type = "latex", 
		  intercept.bottom = FALSE, 
		  keep.stat = c("rsq"), 
		  digits=2, 
		  column.labels =  c("Support (Democrats)","Support (Republicans)", "Extremity (Democrats)", "Extremity (Republicans)"),
		  covariate.labels = c("Intercept", "Bipartisan Treatment", "# Defecting Representatives"))

data$dv<-data$Q2.6
demsupp <- lm(dv~condition +reps, data=data[data$Q4.8<4,])
repsupp <- lm(dv~condition + reps, data=data[data$Q4.8>4,])
data$dv<-data$Q2.6
demideo <- lm(dv~condition + reps, data=data[data$Q4.8<4,])
repideo <- lm(dv~condition + reps, data=data[data$Q4.8>4,])

stargazer(demsupp, repsupp, demideo, repideo,
		  type = "latex", 
		  intercept.bottom = FALSE, 
		  keep.stat = c("rsq"), 
		  digits=2, 
		  column.labels =  c("Support (Liberals)","Support (Conservatives)", "Extremity (Liberals)", "Extremity (Conservatives)"),
		  covariate.labels = c("Intercept", "Bipartisan Treatment", "# Defecting Representatives"))


######
###### Reason
######

data<-read.csv("reason.csv",header=T,as.is=T)

df <- read.csv(text="col1,col2")

#age
df<-rbind(df,cbind("Age",round(mean(data$Q1.4+17,na.rm=T),digits=2)))
#male
df<-rbind(df,cbind("Male",round(prop.table(table(data$Q1.5))[[1]]*100,digits=2)))
#>college
df<-rbind(df,cbind(">College",round(prop.table(table(data$Q1.6>=7))[[2]]*100,digits=2)))
#white
df<-rbind(df,cbind("White",round(prop.table(table(data$Q1.8==4))[[2]]*100,digits=2)))
#ideology
df<-rbind(df,cbind("Ideology",round(mean(data$Q4.8,na.rm=T),digits=2)))
names(df)<-c("Variable"," ")
print(xtable(df),include.rownames=FALSE)


greaterThanZero<- round(100-((nrow(dataReason[dataReason$Q2.7_1==0,])/length(dataReason$Q2.7_1))*100),digits=2)

results<-as.data.frame(cbind(1,greaterThanZero))
names(results)<-c("reason","percent")
results$reason <- factor(results$reason, labels=c("Allocated more than 0%\n responsibility to Democrats"))

p<-length(na.omit(dataReason$reason))
results<- ddply(.data = dataReason, .variables = .(reason), .fun = summarise, 
				percent = round((length(na.omit(reason))/p)*100,digits=2))
results<-results[1:3,]
results
results$reason <- factor(results$reason, labels=c("Other Reason","Part of the system","Implied Action"))
results
